﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using ARR.UI.ViewModels;

namespace ARR.UI.Views
{
    /// <summary>
    /// Interaction logic for ReportsViewer.xaml
    /// </summary>
    public partial class ReportsViewer : Window
    {
        private ReportType _type;
        private int _rangeReviewId;
        private int? _categoryId;

        public ReportsViewer(ReportType type, int rangeReviewId, int? categoryId)
        {
            _type = type;
            _rangeReviewId = rangeReviewId;
            _categoryId = categoryId;

            InitializeComponent();
            this.Loaded += new RoutedEventHandler(ReportsViewer_Loaded);
            this.Closed += new EventHandler(ReportsViewer_Closed);
        }

        void ReportsViewer_Closed(object sender, EventArgs e)
        {
            //TODO: release del file
            this.webBrowser1.Navigate(new Uri("http://localhost"));
        }

        void ReportsViewer_Loaded(object sender, RoutedEventArgs e)
        {
            string fileName = Render();
            this.webBrowser1.Navigate(fileName);
        }

        private string Render()
        {
            //Paso 1: Llamo al Stored Procedure
            ReportsService.ReportsServiceClient reportsService = new ReportsService.ReportsServiceClient();
            byte[] bytes = null;
            string fileName = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), string.Format("{0}{1}{2}.pdf", _type.ToString(), _rangeReviewId, _categoryId));

            switch (_type)
            {
                case ReportType.CategoryReport:
                    {
                        try
                        {
                            bytes = reportsService.GetSalesCategoryComparison(_rangeReviewId, _categoryId.Value);
                            reportsService.Close();
                        }
                        catch
                        {
                            reportsService.Abort();
                            throw;
                        }

                        break;
                    }
                case ReportType.ExecutiveSummary:
                    {
                        try
                        {
                            bytes = reportsService.GenerateExecutiveSummaryReport(_rangeReviewId);
                            reportsService.Close();
                        }
                        catch
                        {
                            reportsService.Abort();
                            throw;
                        }
                        break;
                    }
            }            

            if (bytes != null)
            {
                try
                {
                    //borro el archivo si es que existe
                    if (File.Exists(fileName))
                    {
                        File.Delete(fileName);
                    }

                    using (FileStream fs = new FileStream(fileName, FileMode.Create))
                    {
                        using (BinaryWriter bw = new BinaryWriter(fs))
                        {
                            bw.Write(bytes);
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }

            return fileName;
        }
    }

    public enum ReportType
    {
        CategoryReport = 1,
        ExecutiveSummary = 2
    }
}
